//package com.many.backs.security.service;
//
//import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
//import com.many.backs.service.sys.SysRoleService;
//import com.many.backs.service.sys.SysUserService;
//import com.many.data.entity.SysRole;
//import com.many.data.entity.SysUser;
//import com.many.data.entity.SysUserRole;
//import com.many.data.mapper.SysUserRoleMapper;
//import lombok.AllArgsConstructor;
//import org.springframework.security.core.GrantedAuthority;
//import org.springframework.security.core.authority.SimpleGrantedAuthority;
//import org.springframework.security.core.userdetails.User;
//import org.springframework.security.core.userdetails.UserDetails;
//import org.springframework.security.core.userdetails.UserDetailsService;
//import org.springframework.security.core.userdetails.UsernameNotFoundException;
//import org.springframework.stereotype.Service;
//import org.springframework.util.ObjectUtils;
//
//import java.util.ArrayList;
//import java.util.Collection;
//import java.util.List;
//
///**
// * @ClassName UserDetailsServiceImpl
// * @Description TODD
// * @Author James
// * @Date 2022/8/30
// * @Version 1.0
// **/
//@Service("userDetailsService")
//@AllArgsConstructor
//public class UserDetailsServiceImpl implements UserDetailsService {
//
//    private SysUserService sysUserService;
//
//    private SysRoleService sysRoleService;
//
//    private SysUserRoleMapper sysUserRoleMapper;
//
//    @Override
//    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
//        Collection<GrantedAuthority>authorities=new ArrayList<>();
//        // 从数据库中取出用户信息
//        SysUser user = sysUserService.getOne(new LambdaQueryWrapper<SysUser>().eq(SysUser::getUserName, username));
//
//        //判断用户是否存在
//        if (ObjectUtils.isEmpty(user)){
//            throw new UsernameNotFoundException("用户名不存在");
//        }
//        //添加权限
//        List<SysUserRole> sysUserRoles = sysUserRoleMapper.selectList(new LambdaQueryWrapper<SysUserRole>().eq(SysUserRole::getUserId, user.getUserId()));
//        for (SysUserRole role:sysUserRoles){
//            SysRole sysRole = sysRoleService.getById(role.getRoleId());
//            authorities.add(new SimpleGrantedAuthority(sysRole.getRoleCode()));
//        }
//        return new User(user.getUserName(),user.getPassword(),authorities);
//    }
//}
